package cn.com.inossem.operation;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import cn.com.inossem.activity.BaGuaFabuActivity;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.AddBaGuaInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;

public class AddBaGuaOperation implements Operation {

	// public static final String ADDBAGUA_OPERATION_BEAN =
	// "addbagua_operation_bean";

	public Bundle bundle;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {

		Gson gson = new Gson();
		AddBaGuaInfoBean bean = BaGuaFabuActivity.bean;
		String tojson = gson.toJson(bean).toString().replace("\n", "")
				.replace("\r", "");
		System.out.println("AddBaGuaOperation tojson:" + tojson);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "gossip/addGossip");
		connection.setPostText(tojson);
		System.out.println(tojson);

		String body = connection.execute();

		System.out.println("AddBaGuaOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");
			if (errorFlag == WSConfig.RESULT_SUCESS) {
				System.out.print("修改成功!");

			} else {

			}
			bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS,
					errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}
}
